home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Selection / Multimedia Selection Volume One - CD-ROM / MULTIMEDIA SELECTION____________.ISO / programz / ldb / cbinder.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-25  |  4.9 KB  |  289 lines

  1. /*
  2.  
  3.     cbinder.cpp
  4.     10-25-91
  5.     Copy Binder: Loose Data Binder v 1.5
  6.  
  7.     Copyright 1991
  8.     John W. Small
  9.     All rights reserved
  10.  
  11.     PSW / Power SoftWare
  12.     P.O. Box 10072
  13.     McLean, Virginia 22102 8072 USA
  14.  
  15.     John Small
  16.     Voice: (703) 759-3838
  17.     CIS: 73757,2233
  18.  
  19. */
  20.  
  21. #include "cbinder.hpp"
  22.  
  23.  
  24. /*  Copy Binder Primitives  */
  25.  
  26. void CBinder::Dstore(ostream& os, voiD D)
  27. {
  28.     if (!D)
  29.         return;
  30.     if (sizeofData)  {
  31.         os.write((const char *)D,sizeofData);
  32.         if (!os)
  33.             serror("unable to store CBinder "
  34.                 "node (fixed sized data)");
  35.     }
  36.     else  {
  37.         unsigned i;
  38.         os << (i = (unsigned) strlen((char *)D))
  39.             << endm;
  40.         if (i)
  41.             os.write((char *)D,i);
  42.         if (!os)
  43.             serror("unable to store CBinder "
  44.                 "node (string data)");
  45.     }
  46. }
  47.  
  48. voiD CBinder::Dload(istream& is)
  49. {
  50.     char * D;
  51.  
  52.     if (sizeofData) {
  53.         if ((D = new char[sizeofData])
  54.             != (char *)0)  {
  55.             is.read(D,sizeofData);
  56.             if (!is)  {
  57.                 serror("loading CBinder "
  58.                     "node (fixed sized "
  59.                     "data)");
  60.                 delete D;
  61.                 D = (char *) 0;
  62.             }
  63.         }
  64.         else
  65.             serror("unable to allocate memory "
  66.                 "for CBinder node "
  67.                 "(fixed size data)");
  68.     }
  69.     else  {
  70.         unsigned i;
  71.  
  72.         D = (char *) 0;
  73.         if (!(is >> i >> nextm))
  74.             serror("loading CBinder node "
  75.                 "(string data)");
  76.         else if ((D = new char[i+1])
  77.             != (char *)0)  {
  78.             if (i)  {
  79.                 is.read(D,i);
  80.                 if (!is)  {
  81.                     serror("loading "
  82.                         "CBinder "
  83.                         "node "
  84.                         "(string "
  85.                         "data)");
  86.                     delete D;
  87.                     D = (char *) 0;
  88.                 }
  89.                 else
  90.                     D[i] = '\0';
  91.             }
  92.             else
  93.                 D[i] = '\0';
  94.         }
  95.         else
  96.             serror("unable to allocate memory "
  97.                 "for CBinder node "
  98.                 "(string data)");
  99.     }
  100.  
  101.     return (voiD) D;
  102. }
  103.  
  104. voiD CBinder::Dclone(const voiD D)
  105. {
  106.     voiD cD;
  107.  
  108.     if (!D)
  109.         return voiD0;
  110.     if (sizeofData)  {
  111.         if ((cD = (voiD) new char[sizeofData])
  112.             != voiD0) memcpy(cD,D,sizeofData);
  113.     }
  114.     else if ((cD = (voiD) new char[strlen((char *)D)+1])
  115.         != voiD0) strcpy((char *)cD,(char *)D);
  116.     return cD;
  117.  
  118. }
  119.  
  120. voiD CBinder::Dcopy(voiD D, const voiD S)
  121. {
  122.     if (D && S && D != S)  {
  123.         if (sizeofData)
  124.             memcpy(D,S,sizeofData);
  125.         else
  126.             strcpy((char *)D,(char *)S);
  127.         return D;
  128.     }
  129.     return voiD0;
  130. }
  131.  
  132. ostream& CBinder::store(ostream& os)
  133. {    /* Store CBinder sizeofData field then Binder! */
  134.     if (!(os << sizeofData << endm))
  135.         serror("unable to store CBinder");
  136.     else
  137.         SBinder::store(os);
  138.     return os;
  139. }
  140.  
  141. StreamablE CBinder::load(istream& is, StreamablE InstancE)
  142. {
  143.     unsigned sizeofData;
  144.     int newed = 0;
  145.  
  146.     if (!(is >> sizeofData >> nextm))  {
  147.         lserror("unable to load CBinder "
  148.             "header data",ID_CLASS);
  149.         return StreamablE0;
  150.     }
  151.     if (!InstancE)  {
  152.         if ((InstancE = (StreamablE)
  153.             new CBinder(UNIQUE_STREAMABLE))
  154.             == StreamablE0)  {
  155.             lserror("unable to construct "
  156.                 "CBinder",ID_CLASS);
  157.             return StreamablE0;                
  158.         }
  159.         newed = 1;
  160.     }
  161.     ((CBindeR)InstancE)->construct(sizeofData);
  162.     StreamablE ancestoR =  SBinder::load(is,InstancE);
  163.     if (!ancestoR)  {
  164.         // ((CBindeR)InstancE)->destruct();
  165.         if (newed)
  166.             delete InstancE;
  167.         return StreamablE0;
  168.     }
  169.     return ancestoR;
  170. }
  171.  
  172. CBinder::CBinder(unsigned sizeofData,
  173.     unsigned maxNodes, unsigned limit,
  174.     unsigned delta)
  175.     : SBinder(BDR_OK_FREE,maxNodes,
  176.         limit,delta)
  177. {
  178.     construct(sizeofData);
  179.     setID(ID_CLASS);
  180. }
  181.  
  182. voiD CBinder::atInsCLN(unsigned n, const voiD D)
  183. {
  184.     voiD cD;
  185.  
  186.     if (!atIns(n,cD = Dclone(D)))  {
  187.         Dfree(cD);
  188.         return voiD0;
  189.     }
  190.     return cD;
  191. }
  192.  
  193. voiD CBinder::atFreeCPY(unsigned n, voiD D)
  194. {
  195.     return (Dcopy(D,atGet(n))? atFree(n), D : voiD0);
  196. }
  197.  
  198. voiD CBinder::atPutCLN(unsigned n, const voiD D)
  199. {
  200.     voiD oldD, cD;
  201.  
  202.     if ((oldD = atGet(n)) == voiD0)
  203.         return voiD0;
  204.     if (oldD == D)
  205.         return voiD0;
  206.     if (!atPut(n,cD = Dclone(D)))  {
  207.         Dfree(cD);
  208.         return voiD0;
  209.     }
  210.     Dfree(oldD);
  211.     return cD;
  212. }
  213.  
  214. CBinder::operator char *()
  215. {
  216.     return (char *) (sizeofData? 0 : current());
  217. }
  218.  
  219. voiD CBinder::pushCLN(const voiD D)
  220. {
  221.     voiD cD;
  222.  
  223.     if (!push(cD = Dclone(D)))  {
  224.         Dfree(cD);
  225.         return voiD0;
  226.     }
  227.     return cD;
  228. }
  229.  
  230. voiD CBinder::popFreeCPY(voiD D)
  231. {
  232.     return (Dcopy(D,atGet(0))? atFree(0), D : voiD0);
  233. }
  234.  
  235. voiD CBinder::insQCLN(const voiD D)
  236. {
  237.     voiD cD;
  238.  
  239.     if (!insQ(cD = Dclone(D)))  {
  240.         Dfree(cD);
  241.         return voiD0;
  242.     }
  243.     return cD;
  244. }
  245.  
  246. voiD CBinder::rmQFreeCPY(voiD D)
  247. {
  248.     return (Dcopy(D,atGet(0))? atFree(0), D : voiD0);
  249. }
  250.  
  251. voiD CBinder::unQFreeCPY(voiD D)
  252. {
  253.     return (Dcopy(D,bottom())? unQFree(), D : voiD0);
  254. }
  255.  
  256. voiD CBinder::insCLN(const voiD D)
  257. {
  258.     voiD cD;
  259.  
  260.     if (!ins(cD = Dclone(D)))  {
  261.         Dfree(cD);
  262.         return voiD0;
  263.     }
  264.     return cD;
  265. }
  266.  
  267. voiD CBinder::insSortCLN(const voiD D)
  268. {
  269.     voiD cD;
  270.  
  271.     if (!insSort(cD = Dclone(D)))  {
  272.         Dfree(cD);
  273.         return voiD0;
  274.     }
  275.     return cD;
  276. }
  277.  
  278. voiD CBinder::delFreeCPY(voiD D)
  279. {
  280.     return (Dcopy(D,current())? delFree(), D : voiD0);
  281. }
  282.  
  283. voiD CBinder::nextCPY(voiD D)
  284.     { return (D? Dcopy(D,next()) : voiD0); }
  285.  
  286. voiD CBinder::prevCPY(voiD D)
  287.     { return (D? Dcopy(D,prev()) : voiD0); }
  288.  
  289.